package com.hlingsoft.kkh.action;

import com.hlingsoft.kkh.dao.*;
import com.hlingsoft.kkh.entity.*;
import com.hlingsoft.kkh.entity.vo.ShareVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by Administrator on 2015/2/15.
 */


@Controller
@RequestMapping("/share")
public class ShareAction extends BaseAction {




    /**
     * 分享瞬间
     *
     * @param eventsId
     * @param imageId
     * @param eventCommentsId
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/moment.htm")
    public String moment(
            @RequestParam(value = "eventsId") long eventsId,
            @RequestParam(value = "imageId") long imageId,     //分享的图片
            @RequestParam(value = "eventCommentsId", defaultValue = "0", required = false) long eventCommentsId,
            HttpServletRequest request, ModelMap modelMap) {
        List<ShareVo> shareVoList = new ArrayList<ShareVo>();

        Image image = imageDao.getImageById(imageId);
        if (eventCommentsId == 0) {
            EventComments eventComments = eventCommentsDao.getFirstEventComments(eventsId);
            eventCommentsId = eventComments.getId();
        }

        //获取EventComments
        EventComments eventComments = eventCommentsDao.getCommentsListById(eventCommentsId);

        //获取events
        Events events = eventsDao.getEventsById(eventComments.getEventId());


        //创建活动人信息
        AppUser eventsUser = userDao.getUserById(events.getAppUserId());

        List<EventComments> eventCommentsList = eventCommentsDao.getNoImageCommentsListByEventId(eventComments.getEventId(),0,100);

        for (EventComments eventComments1 : eventCommentsList) {
            ShareVo shareVo = new ShareVo();
            AppUser user = userDao.getUserById(eventComments1.getAppUserId());
            shareVo.setHeadImage(user.getHeadImage());
            shareVo.setName(user.getName());
            shareVo.setContent(eventComments1.getContent());
            shareVo.setCreateTime(eventComments1.getUpdateTime());
            shareVoList.add(shareVo);
        }

        modelMap.put("eventsUser", eventsUser);
        modelMap.put("events", events);
        modelMap.put("shareVoList", shareVoList);
        modelMap.put("image", image);

        return "system/share/default";
    }


    /**
     * 图片显示
     *
     * @param eventsId
     * @param request
     * @return
     */
    @RequestMapping(value = "/showImages.json")
    @ResponseBody
    public Map<String, Object> shareEvents(
            @RequestParam(value = "eventsId") long eventsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<Image> imageList = imageDao.getImageByEventsId(eventsId);

            Collections.sort(imageList, new Comparator<Image>() {
                @Override
                public int compare(Image o1, Image o2) {
                    return o1.getTokeTime().compareTo(o2.getTokeTime());
                }
            });

            resultMap.put("imageList", imageList);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;
    }


    /**
     * 分享活动
     * @param userId
     * @param eventsId

     * @param request
     * @return
     */
    @RequestMapping(value = "/events.json")
    @ResponseBody
    public Map<String, Object> share(
            @RequestParam(value = "userId", defaultValue = "0") long userId,
            @RequestParam(value = "eventsId") long eventsId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {

            Share share = new Share();
            share.setMyId(userId);
            share.setEventsId(eventsId);
            Events events = eventsDao.getEventsById(eventsId);
            share.setAppUserId(events.getAppUserId());
            share.setEventCommentsId(0);
            Date now = new Date();
            share.setCreateTime(now);
            share.setUpdateTime(now);
            share.setAvailable(1);

            shareDao.addShare(share);
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }




    /**
     * 分享events
     *
     * @param eventsId
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/events.htm")
    public String shareEvents(
            @RequestParam(value = "eventsId") long eventsId,
            @RequestParam(value = "imageIds") String imageIds,
            HttpServletRequest request, ModelMap modelMap) {

        String[] sourceStrArray = imageIds.split(",");
        //分享的图片
        List<Image> imageList = new ArrayList<Image>();
        for (String string:sourceStrArray){
            long thelong = Long.valueOf(string);
            Image image = imageDao.getImageById(thelong);
            imageList.add(image);
        }



        List<ShareVo> shareVoList = new ArrayList<ShareVo>();
        //获取events记录
        Events events = eventsDao.getEventsById(eventsId);
        //获取活动发布人信息
        AppUser eventsUser = userDao.getUserById(events.getAppUserId());


        //文字评论
        List<EventComments> eventCommentsList = eventCommentsDao.getNoImageCommentsListByEventId(eventsId,0,100);
        for (EventComments eventComments1 : eventCommentsList) {
            ShareVo shareVo = new ShareVo();
            AppUser user = userDao.getUserById(eventComments1.getAppUserId());
            shareVo.setHeadImage(user.getHeadImage());
            shareVo.setName(user.getName());
            shareVo.setContent(eventComments1.getContent());
            shareVo.setCreateTime(eventComments1.getUpdateTime());
            shareVoList.add(shareVo);
        }

        modelMap.put("eventsUser", eventsUser);
        modelMap.put("events", events);
        modelMap.put("imageList", imageList);
        modelMap.put("shareVoList", shareVoList);

        return "system/share/eventsDetail";
    }

}
